package net.lzzy.practicesapi1.web.admin;

import net.lzzy.practicesapi1.constants.ApiConstant;
import net.lzzy.practicesapi1.constants.Messages;
import net.lzzy.practicesapi1.entities.User;
import net.lzzy.practicesapi1.services.UserService;
import net.lzzy.practicesapi1.web.ApiResult;
import net.lzzy.practicesapi1.web.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 梁园钲
 */
@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_USER_ROOT)
public class UserApiController {
    private final UserService service;

    @Autowired
    public UserApiController(UserService service) {
        this.service = service;
    }

    @GetMapping(ApiConstant.ROUTE_USER_ALL)
    public ApiResult getUser(@RequestParam String token) {
        List<VmUser> users = new ArrayList<>();
        service.get().forEach(user -> users.add(VmUser.create(user, true)));
        return new ApiResult(true, users.size() + "", users);
    }

    @GetMapping(ApiConstant.ROUTE_USER_PAGE)
    public ApiResult getPagedUsers(@RequestParam int page, @RequestParam int size, @RequestParam String token) {
        List<VmUser> users = new ArrayList<>();
        service.get(page, size).forEach(user -> users.add(VmUser.create(user, true)));
        return new ApiResult(true, users.size() + "", users);
    }

    @GetMapping(ApiConstant.ROUTE_USER_SEARCH)
    public ApiResult search(@RequestParam String token, @PathVariable String kw) {
        List<VmUser> users = new ArrayList<>();
        service.search(kw).forEach(user -> users.add(VmUser.create(user, true)));
        return new ApiResult(true, users.size() + "", users);
    }

    @GetMapping(ApiConstant.ROUTE_USER_APPLY)
    public ApiResult getApplyTeacher(@RequestParam String token) {
        List<VmUser> users = new ArrayList<>();
        service.getApplyTeacher(true).forEach(user -> users.add(VmUser.create(user, true)));
        return new ApiResult(true, users.size() + "", users);
    }
    @PostMapping(ApiConstant.ROUTE_USER_APPROVE)
    public ApiResult approve(@RequestParam String token, @RequestParam UUID id){
        User user = service.approveTeacher(id);
        if(user == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmUser.create(user,true));
    }
    @PostMapping(ApiConstant.ROUTE_USER_DECLINE)
    public ApiResult decline(@RequestParam String token,@RequestParam UUID id){
        User user = service.declineTeacher(id);
        if(user == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmUser.create(user,true));
    }
    @PostMapping(ApiConstant.ROUTE_USER_BAN)
    public ApiResult ban(@RequestParam String token,@RequestParam UUID id){
        User user = service.banUser(id);
        if(user == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmUser.create(user,true));
    }
}
